import { _decorator, Collider2D, Component, Contact2DType, IPhysics2DContact, Node, sp, Vec3 } from 'cc';
import { ZTool } from '../AshFramework/Utils/ZTool';
import { MapCamera } from './MapCamera';
import { Constant } from '../AshFramework/Datas/Constant';
import { EnemyController } from './EnemyController';
import PlayerController from '../PlayerController';
import { AudioManager, Audios } from '../AshFramework/Managers/AudioManager';
const { ccclass, property } = _decorator;

enum Ani {
    None = "",
    Boom = "baozha_texiao",
}

@ccclass('Explosion_Bullet')
export class Explosion_Bullet extends Component {

    Collider: Collider2D = null;
    Skeleton: sp.Skeleton = null;
    State: string = "";
    CB: Function = null;

    Harm: number = 0;
    Armor: number = 0;

    protected onLoad(): void {
        this.Collider = this.node.getComponent(Collider2D);
        this.Skeleton = this.node.getComponent(sp.Skeleton);
        this.Skeleton.setCompleteListener((trackEntry: sp.spine.TrackEntry) => { this.CB && this.CB() });
        this.Collider.on(Contact2DType.BEGIN_CONTACT, this.onBeginContact, this);

    }

    show(worldPos: Vec3, angle: number, harm: number = 0, armor: number = 0) {
        this.Harm = harm;
        this.Armor = armor;
        this.node.setWorldPosition(worldPos);
        const randAngle = ZTool.GetRandom(-5, 5);
        this.node.angle = angle + randAngle;
        MapCamera.Instance.shakeCamera(4, 0.025, 10);
        AudioManager.PlaySound(Audios.Boom);
        this.playAni(Ani.Boom, false, () => {
            this.node.destroy();
        })
    }

    playAni(ani: string, loop: boolean = false, cb: Function = null) {
        if (this.State === ani) return;
        this.State = ani;
        if (this.State == Ani.None) {
            this.Skeleton.timeScale = 0;
            return;
        }
        this.Skeleton.timeScale = 1;
        this.CB = cb;
        this.Skeleton.setAnimation(0, ani, loop);
    }

    onBeginContact(selfCollider: Collider2D, otherCollider: Collider2D, contact: IPhysics2DContact | null) {
        if (otherCollider.group == Constant.Group.ENEMY) {
            otherCollider.node.getComponent(EnemyController).hit(this.Harm, this.Armor);
        } else if (otherCollider.group == Constant.Group.PLAYER) {
            PlayerController.Instance.hit(this.Harm);
        }
    }
}


